/*
 * SAMPLE CODE AT BOTTOM!!!
 *
 * You need to put the name and values in quotes when you call the function, like this:
 * set_cookie( 'mycookie', 'visited 9 times', 30, '/', '', '' );. Don't forget to put in empty quotes for the unused parameters or
 * you'll get an error when you run the code. This makes the cookie named 'mycookie', with the value of 'visited 9 times', and with 
 * a life of 30 days, and the cookie is set to your root folder.
 *
 * The Set_Cookie values for 'domain' and 'secure' are not utilized. Use 'domain' on the Javascript cookie if you are using it on a 
 * subdomain, like widgets.yoursite.com, where the cookie is set on the widgets subdomain, but you need it to be accessible over the
 * whole yoursite.com domain.
 *
 * It's good practice to not assume the path to the site root will be set the way you want it by default, so do this manually as a 
 * rule, '/'. If no value is set for expires, it will only last as long as the current session of the visitor, and will be automatically 
 * deleted when they close their browser. 
 */

function set_cookie(name, value, expires, path, domain, secure) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
		//expires = expires * 1000 * 60;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

/*
 * This will retrieve the cookie by name, if the cookie does not exist, it will return false, so you can do things like 
 * if ( Get_Cookie( 'your_cookie' ) ) do something.
 */

function get_cookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length )))
	{
		return null;
	}
	if (start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

/*
 * Here all you need to do is put in: Delete_Cookie('cookie name', '/', '') and the cookie will be deleted. Remember to match 
 * the cookie name, path, and domain to what you have it in Set_Cookie exactly, or you may get some very hard to diagnose errors.
 */

// this deletes the cookie when called
function Delete_Cookie(name, path, domain) {
	if(Get_Cookie(name)) document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
};
Ext.app.App = function(cfg) {
	Ext.apply(this, cfg);
	this.addEvents( {
		'ready' :true,
		'beforeunload' :true
	});

	Ext.onReady(this.initApp, this);
};

Ext.extend(Ext.app.App, Ext.util.Observable, {
	isReady :false,
	startMenu :null,
	modules :null,

	getStartConfig : function() {
	
	},

	initApp : function() {
		this.startConfig = this.startConfig || this.getStartConfig();
		this.desktop = new Ext.Desktop(this);

		this.launcher = this.desktop.taskbar.startMenu;

		this.modules = this.getModules();
		if (this.modules) {
			this.initModules(this.modules);
			this.initDesktopConfig();
		}

		this.init();

		
		Ext.EventManager.on(window, 'beforeunload', this.onUnload, this);
		//Ext.EventManager.on(window, 'contextmenu', this.onMenu, this);
		this.fireEvent('ready', this);
		this.isReady = true;
	},

	getModules :Ext.emptyFn,
	init :Ext.emptyFn,
	getDesktopConfig : Ext.emptyFn,
	initDesktopConfig : function(){
		var o = {"styles": {"wallpaperfile":"/wallpapers/desktop.jpg","transparency":true,"themefile":"/css/xtheme-vistaglass/css/xtheme-vistaglass.css","wallpaperid":7,"themename":"Vista Glass","wallpaperposition":"center","wallpapername":"Fresh Morning","themeid":3,"fontcolor":"FFFFFF","backgroundcolor":"f9f9f9"}};
		this.desktop.config = o;
		//this.desktop.initialConfig = o;
		//this.initStyles(o.styles);	
    },
	
	initModules : function(ms) {
		for ( var i = 0, len = ms.length; i < len; i++) {
			var m = ms[i];
			this.launcher.add(m.launcher);
			m.app = this;
		}
	},

	getModule : function(name) {
		var ms = this.modules;

		for ( var i = 0, len = ms.length; i < len; i++) {
			if (ms[i].id == name || ms[i].appType == name) {
				return ms[i];
			}
		}
		return '';
	},
	
	initStyles : function(s){
    	this.desktop.setBackgroundColor(s.backgroundcolor);
    	this.desktop.setFontColor(s.fontcolor);
    	this.desktop.setTransparency(s.transparency);
    	this.desktop.setWallpaperPosition(s.wallpaperposition);
    	
    	/** 设置cookie*/
    	if ( get_cookie( 'CSSAndPICCOOKIE' ) ){
			var themePath = get_cookie( 'CSSAndPICCOOKIE').split("&")[0];
			var picPath = get_cookie( 'CSSAndPICCOOKIE').split("&")[1];
	    	this.desktop.setTheme({
    		id: 2,
    		name: '333',
    		pathtofile: themePath
    	});
	    	
	    	if( picPath =='0')
	    	{
	    		picPath = s.wallpaperfile;
	    	}
	    	this.desktop.setWallpaper({
	    		id: 8,
	    		name: 'picName',
	    		pathtofile: picPath
	    	});
			
		}else{
			
	    	this.desktop.setWallpaper({
	    		id: s.wallpaperid,
	    		name: s.wallpapername,
	    		pathtofile: s.wallpaperfile
	    	});
		}



    },
	onReady : function(fn, scope) {
		if (!this.isReady) {
			this.on('ready', fn, scope);
		} else {
			fn.call(scope, this);
		}
	},

	getDesktop : function() {
		return this.desktop;
	},

	onUnload : function(e) {
		if (this.fireEvent('beforeunload', this) === false) {
			e.stopEvent();
		}
	}
});